$(function(){
	
	var coverList = $('#coverInfoList')
    var houseList = $('#houseInfoList');
	
	var link_province = initProvice();
    var coverTr = '<tr>'+
      '<td>'+
      /*'  <select name="link_type" class="form-control">'+
      '    <option value="联通">联通</option>'+
      '    <option value="移动">移动</option>'+
      '    <option value="电信">电信</option>'+
      '    <option value="其他">其他</option>'+
      '  </select>'+*/
      '  <input name="link_type"  type="text" class="form-control input-sm" />'+
      '</td>'+
      '<td>'+
      '  <input name="link_bandwidth"  type="text" class="form-control input-sm" />'+
      '</td>'+
      '<td>'+
      '  <select name="link_province" id="link_province" class="form-control">'+
             link_province +
      '  </select>'+
      '<td>'+
      '  <input type="text" class="form-control input-sm" />'+
      '</td>'+
      '</td>'+
      '<td class="delete">'+
      '  -'+
      '</td>'+
    '</tr>';
    coverList.append(coverTr)
    
    
    $('#addCoverInfo').on('click', function(){
      var link_province = initProvice();
      var coverTr = '<tr>'+
        '<td>'+
        /*'  <select name="link_type" class="form-control">'+
        '    <option value="联通">联通</option>'+
        '    <option value="移动">移动</option>'+
        '    <option value="电信">电信</option>'+
        '    <option value="其他">其他</option>'+
        '  </select>'+*/
        '  <input name="link_type" type="text" class="form-control input-sm" />'+
        '</td>'+
        '<td>'+
        '  <input name="link_bandwidth" type="text" class="form-control input-sm" />'+
        '</td>'+
        '<td>'+
        '  <select name="link_province" id="link_province" class="form-control">'+
               link_province +
        '  </select>'+
        '</td>'+
        '<td>'+
        '  <input type="text" class="form-control input-sm" />'+
        '</td>'+
        '<td class="delete">'+
        '  -'+
        '</td>'+
      '</tr>';
      coverList.append(coverTr)
    })
    
    var house_province = initProvice();
    var houserTr = '<tr>'+
        '<td>'+
        '  <select id="house_province" onchange="changeProvince(this,0)" class="form-control">'+
        		house_province +
        '  </select>'+
        '</td>'+
        '<td>'+
        '  <select id="house_city_0" class="form-control"/></select>'+
        '</td>'+
        '<td>'+
        '  <input name="house_name" type="text" class="form-control input-sm" />'+
        '</td>'+
        '<td>'+
        '  <input name="house_add" type="text" class="form-control input-sm" />'+
        '</td>'+
        '<td class="delete">'+
        '  -'+
        '</td>'+
      '</tr>';
      houseList.append(houserTr)
      
      var obj = new Object();
      obj.value = '110000';
      changeProvince(obj,'0');
    
    var house_index = 1;
    $('#addHouseInfo').on('click', function(){
    	house_index++;
    	var house_province = initProvice();
        var house_city = "house_city_"+house_index;
    	var houserTr = '<tr>'+
        '<td>'+
        '  <select id="house_province" onchange="changeProvince(this,'+house_index+')" class="form-control">'+
        		house_province +
        '  </select>'+
        '</td>'+
        '<td>'+
        '  <select id='+house_city+' class="form-control"/></select>'+
        '</td>'+
        '<td>'+
        '  <input type="text" class="form-control input-sm" />'+
        '</td>'+
        '<td>'+
        '  <input type="text" class="form-control input-sm" />'+
        '</td>'+
        '<td class="delete">'+
        '  -'+
        '</td>'+
      '</tr>';
      houseList.append(houserTr);
      
      var obj1 = new Object();
      obj1.value = '110000';
      changeProvince(obj1,house_index);
      
    })
    
    $('#coverInfoList ').on('click','.delete', function(){
      $(this).parent('tr').remove()
    })
    $('#houseInfoList').on('click', '.delete', function(){
      $(this).parent('tr').remove()
    })
    
    $('#submit').click(function (e) {
    	
    	var link_info = '';
    	$('#coverInfoList tr').each(function() {
			//var link_type = $(this).find('td').eq(0).find("select").val()
    		var link_type = $(this).find('td').eq(0).find("input:text").val();
			var link_bandwidth = $(this).find('td').eq(1).find("input:text").val();
			var link_province =$(this).find('td').eq(2).find("option:selected").text()
			var house_name = $(this).find('td').eq(3).find("input:text").val();
			link_info +=link_type+"-"+link_bandwidth+"-"+link_province+"-"+house_name+"@";
		});  
    	
    	var house_info = '';
    	$('#houseInfoList tr').each(function() {
			var house_province = $(this).find('td').eq(0).find("select").val()
			var house_city = $(this).find('td').eq(1).find("select").val()
			var house_name = $(this).find('td').eq(2).find("input:text").val();
			var house_add = $(this).find('td').eq(2).find("input:text").val();
			house_info +=house_province+"-"+house_city+"-"+house_name+"-"+house_add+"@";
		});  
    	
    	layer.load(4);
    	$.ajax({
    		type : "POST",
    		url : "evalResultInfo/save",
    		data : {
    			"enterprise_name" : $("#enterprise_name").val(),
    			"apply_business" : $("#apply_business").val(),
    			"registered_address" : $("#registered_address").val(),
    			"handleInfo" : $("#handleInfo").val(),
    			"contacts" : $("#contacts").val(),
    			"tel" : $("#tel").val(),
    			"moveTle" : $("#moveTle").val(),
    			"eMail" : $("#eMail").val(),
    			"production_unit" : $("#production_unit").val(),
    			"link_info" : link_info,
    			"house_info" : house_info
    		},
    		dataType : "json",
    		success : function(data) {
    			layer.closeAll();
    			if (data.flag) {
    				alert(data.msgs);
    			} else {
    				alert(data.msgs);
    			}
    		},
    		error : function(data) {
    			layer.closeAll();
    		}
    	});
    	
    });
    
    
    
    
    
});

function btn_submit(){
	
	var link_form= $('#link_form').serialize();  
	alert(link_form)
	
}

function initProvice(){
	var province = '';
	$.ajax({
		type:"POST",
		url:"province/getList",
		async:false,
		dataType:'json',
		success:function(result){
			if(result){
				for(var i=0; i<result.length; i++){
					province += '<option value="'+result[i].provinceid+'">'+result[i].province+'</option>';
				}
			}
		}
	});
	return province;
}

function changeProvince(obj,index){
	$.ajax({
		type:"POST",
		async:false,
		url:"cities/getByProvinceid",
		data: {
			'provinceid' : obj.value
		},
		async:false,
		dataType:'json',
		success:function(result){
			if(result){
				var house_city = $("#house_city_"+index);
				house_city.empty();
				var str = '';
				for(var i=0; i<result.length; i++){
					str += '<option value="'+result[i].city+'">'+result[i].city+'</option>';
				}
				house_city.append(str);
			}
		}
	});
}

